//首先正则验证，再存cookie
function setCookie(key, value, timing) {
    var date = new Date();
    date.setDate(date.getDate() + timing);
    document.cookie = key + "=" + value + ";expires=" + date;
}
function getCookie(key) {
    var arr = document.cookie.split("; ");
    for (var i = 0; i < arr.length; i++) {
        var arr2 = arr[i].split("=");
        if (key === arr2[0]) {
            return arr2[1]
        }
    }
}
function randomCodes(){
    var str="1234567890";
    var codes="";
    for(var i=0;i<4;i++){
        var index=parseInt(Math.random()*str.length);
    codes+=str[index];
    }
    return codes;
}

//获取元素
var userNew = getCookie('users') ? JSON.parse(getCookie('users')) : [];
var userName = document.querySelector(".user");
var userTip = document.querySelector(".usernameTip");
// 一次密码输入
var fspwd = document.querySelector(".firstpwd");
// 二次密码输入
var pwd = document.querySelector(".password");
var pwdTip = document.querySelector(".pwdTip");
// 电话
var tel = document.querySelector(".tel");
var telTip=document.querySelector(".telTip");
// 验证码
var code = document.querySelector(".yanzhengma");
var randomCode=document.querySelector(".randomCode");
// 正则
randomCode.innerHTML= randomCodes();

var telReg=/^1[3-9]{1}\d{9}$/;
var userReg = /^[a-z_](\w|\.){3,7}$/i;
var pwdReg = /^\w{6,10}$/;
var obj = { user: false, pwd: false,tel:false,code:false }   //有两个开关，用对象来初始化开关
userName.onblur = function () {  //当输入失去焦点时，判断是否符合格式
    if (userReg.test(this.value)) {
        userTip.innerHTML = "符合要求";
        obj.user = "true";   //当条件满足时，改变开关
        userTip.style.background="#fff";
    } else {
        userTip.innerHTML = "请输入符合要求的用户名";
        userTip.style.background="#ff6700";
    }
}
pwd.onblur = function () {
    if (this.value == fspwd.value) {
        pwdTip.innerHTML = "符合要求";
        obj.pwd = "true";    //当条件满足时，改变开关
        pwdTip.style.background="#fff";
    } else {
        pwdTip.getElementsByClassName.color="red"
        pwdTip.innerHTML = "密码前后不一致";
        pwdTip.style.background="#ff6700";
    }
}
tel.onblur=function(){
    if (telReg.test(this.value)) {
        telTip.innerHTML = "手机号码格式符合要求";
        obj.tel = "true";    //当条件满足时，改变开关
        telTip.style.background="#fff";
    } else {
        telTip.innerHTML = "请填写正确的手机号";
        telTip.style.background="#ff6700";
    }
}
var num=randomCode.innerHTML;
randomCode.onclick=function(){
    randomCode.innerHTML= randomCodes();
    num=randomCode.innerHTML;
}
code.onblur=function(){
    if(this.value==num){
        obj.code="true"
        
    }
    console.log(obj.code)
}
var btn = document.querySelector(".submit")
btn.onclick = function () {
    if (obj.user && obj.pwd && obj.tel && obj.code) {  
       var person = {
           personName:userName.value,
           personpwd:pwd.value,
           state:0
       }
       //   判断有没有重复
       var res = userNew.some(function (el) {
           return el.personName === userName.value;
       })
       if (res) {
           alert('用户名已存在,请直接去登录');
           return;
       }
       userNew.unshift(person);
       setCookie("users", JSON.stringify(userNew),5)
       this.value="注册成功，即将进入登录页面..."
       setTimeout(function(){
           location.href="./login.html";
       },1500)
    }
    else{
        alert('输入错误')
    }  
}





